<script>
import {
  Image as ImageLayer,
  Tile as TileLayer,
  Vector as VectorLayer
} from "ol/layer";
import ImageWMS from "ol/source/ImageWMS";
import VectorSource from "ol/source/Vector";
import OSM from "ol/source/OSM";
import {
  defaults as defaultControls,
  OverviewMap,
  ScaleLine,
  Attribution,
  MousePosition
} from "ol/control";
import { Stroke, Style } from "ol/style";
import GeoJSON from "ol/format/GeoJSON";
import { bbox as bboxStrategy } from "ol/loadingstrategy";
import { createStringXY } from "ol/coordinate";

/* 全局变量声明 */
var Global = Global || {};
Global.namespace = function(str) {
  // 命名空间函数，避免成员变量命名冲突
  var parts = str.split("."),
    parent = Global;
  for (var i = parts[0] == "Global" ? 1 : 0; i < parts.length; i++) {
    parent[parts[i]] = parent[parts[i]] || {};
    parent = parent[parts[i]];
  }
  return parent;
};

Global.namespace("method");
Global.namespace("option");
Global.namespace("style");
Global.namespace("color");
Global.namespace("lineWidth");
Global.namespace("opacity");
Global.namespace("unit");
Global.namespace("constant");
Global.namespace("locus.method");
Global.namespace("locus.map");
Global.namespace("locus.layers");
Global.namespace("locus.layers.elevationLayer");
Global.namespace("locus.controls");
Global.namespace("locus.draw");
Global.namespace("locus.draw.cable");
Global.namespace("locus.select");
Global.namespace("locus.copy");
Global.namespace("locus.copy.features");
Global.namespace("locus.profile");
Global.namespace("locus.label");
Global.namespace("locus.flag");
Global.namespace("locus.screen");
Global.namespace("locus.save");
Global.namespace("locus.constant");
Global.namespace("locus.mousePositionCtl");
Global.namespace("locus.toPipe");

Global.namespace("line.initVar");
Global.namespace("line.method");
Global.namespace("line.map");
Global.namespace("line.layers");
Global.namespace("line.controls");
Global.namespace("line.draw");
Global.namespace("line.select");
Global.namespace("line.unit");
Global.namespace("line.save");
Global.namespace("line.constant");
Global.namespace("line.copy");
Global.namespace("line.flag");

Global.namespace("wire.initVar");
Global.namespace("wire.method");
Global.namespace("wire.map");
Global.namespace("wire.layers");
Global.namespace("wire.controls");
Global.namespace("wire.draw");
Global.namespace("wire.select");
Global.namespace("wire.unit");
Global.namespace("wire.save");
Global.namespace("wire.constant");
Global.namespace("wire.copy");
Global.namespace("wire.flag");

Global.option.ip = "localhost";

// 底图
Global.locus.layers.baseLayer = new ImageLayer({
  source: new ImageWMS({
    url: "http://" + Global.option.ip + ":8082/geoserver/tongxiang/wms",
    params: { LAYERS: "tongxiang:basemap" },
    ratio: 1,
    serverType: "geoserver"
  })
});

// 电气设备
Global.locus.layers.electricalLayer = new VectorLayer({
  source: new VectorSource({
    format: new GeoJSON(),
    url: function(extent) {
      return (
        "http://" +
        Global.option.ip +
        ":8082/geoserver/tongxiang/wfs?service=WFS&" +
        "version=1.1.0&request=GetFeature&typename=tongxiang:geo_electrical&" +
        "outputFormat=application/json&srsname=EPSG:4326&" +
        "bbox=" +
        extent.join(",") +
        ",EPSG:4326"
      );
    },
    strategy: bboxStrategy
  }),
  style: new Style({
    stroke: new Stroke({
      color: "#0080ff",
      width: 2
    })
  })
});

// 土建设备
Global.locus.layers.buildingLayer = new VectorLayer({
  source: new VectorSource({
    format: new GeoJSON(),
    url: function(extent) {
      return (
        "http://" +
        Global.option.ip +
        ":8082/geoserver/tongxiang/wfs?service=WFS&" +
        "version=1.1.0&request=GetFeature&typename=tongxiang:geo_civil_engineering&" +
        "outputFormat=application/json&srsname=EPSG:4326&" +
        "bbox=" +
        extent.join(",") +
        ",EPSG:4326"
      );
    },
    strategy: bboxStrategy
  }),
  style: new Style({
    stroke: new Stroke({
      color: "#ff8000",
      width: 2
    })
  })
});

// 土建测绘
Global.locus.layers.surveyLayer = new VectorLayer({
  source: new VectorSource({
    format: new GeoJSON(),
    url: function(extent) {
      return (
        "http://" +
        Global.option.ip +
        ":8082/geoserver/tongxiang/wfs?service=WFS&" +
        "version=1.1.0&request=GetFeature&typename=tongxiang:geo_point_survery&" +
        "outputFormat=application/json&srsname=EPSG:4326&" +
        "bbox=" +
        extent.join(",") +
        ",EPSG:4326"
      );
    },
    strategy: bboxStrategy
  }),
  style: new Style({
    stroke: new Stroke({
      color: "#008000",
      width: 2
    })
  })
});

// 剖面
Global.locus.layers.profileLayer = new VectorLayer({
  source: new VectorSource({
    format: new GeoJSON(),
    url: function(extent) {
      return (
        "http://" +
        Global.option.ip +
        ":8082/geoserver/tongxiang/wfs?service=WFS&" +
        "version=1.1.0&request=GetFeature&typename=tongxiang:geo_pipe_profile&" +
        "outputFormat=application/json&srsname=EPSG:4326&" +
        "bbox=" +
        extent.join(",") +
        ",EPSG:4326"
      );
    },
    strategy: bboxStrategy
  }),
  style: new Style({
    stroke: new Stroke({
      color: "#004040",
      width: 2
    })
  })
});

// 剖面属性
Global.locus.layers.pipe_propertyLayer = new VectorLayer({
  source: new VectorSource({
    format: new GeoJSON(),
    url: function(extent) {
      return (
        "http://" +
        Global.option.ip +
        ":8082/geoserver/tongxiang/wfs?service=WFS&" +
        "version=1.1.0&request=GetFeature&typename=tongxiang:geo_pipe_hole&" +
        "outputFormat=application/json&srsname=EPSG:4326&" +
        "bbox=" +
        extent.join(",") +
        ",EPSG:4326"
      );
    },
    strategy: bboxStrategy
  }),
  style: new Style({
    stroke: new Stroke({
      color: "#800040",
      width: 2
    })
  })
});

// 剖面电缆
Global.locus.layers.cableLayer = new VectorLayer({
  source: new VectorSource({
    format: new GeoJSON(),
    url: function(extent) {
      return (
        "http://" +
        Global.option.ip +
        ":8082/geoserver/tongxiang/wfs?service=WFS&" +
        "version=1.1.0&request=GetFeature&typename=tongxiang:geo_cable&" +
        "outputFormat=application/json&srsname=EPSG:4326&" +
        "bbox=" +
        extent.join(",") +
        ",EPSG:4326"
      );
    },
    strategy: bboxStrategy
  }),
  style: new Style({
    stroke: new Stroke({
      color: "#8080ff",
      width: 2
    })
  })
});

// 缩略图控件
Global.locus.controls.overView = new OverviewMap({
  className: "ol-overviewmap ol-custom-overviewmap",
  layers: [
    new ImageLayer({
      source: new ImageWMS({
        url: "http://" + Global.option.ip + ":8082/geoserver/tongxiang/wms",
        params: { LAYERS: "tongxiang:planet_osm_line" },
        ratio: 1,
        serverType: "geoserver"
      })
    })
  ],
  collapseLabel: "\u00BB",
  label: "\u00AB",
  collapsed: true
});

// 比例尺控件
Global.locus.controls.scaleLine = new ScaleLine({
  units: "metric"
});

// 位置控件
Global.locus.controls.mousePosition = new MousePosition({
  className: "ol-custom-mouse-position",
  //投影坐标格式，显示小数点后边多少位
  coordinateFormat: createStringXY(8),
  //指定投影
  projection: "EPSG:4326"
  //目标容器
  // target: document.getElementById("myposition")
});

export default Global;
</script>